package com.rdgk.newtech.controller;

import com.alibaba.fastjson.JSONObject;
import com.rdgk.newtech.service.StatisticalService;
import com.rdgk.newtech.util.CommonUtil;
import com.rdgk.newtech.util.WordUtil;
import com.rdgk.newtech.util.constants.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: 韩怀亮
 * @description: 统计列表接口Controller
 */
@RestController
@RequestMapping("/statistical")
public class StatisticalController {

	@Autowired
	private StatisticalService service;


	/**
	 * 查询专家列表
	 */
	@PostMapping("/getAuthorList")
	public JSONObject getAuthorList(HttpServletRequest request) {
		return service.getAuthorList(CommonUtil.request2Json(request));
	}
	/**
	 * 获取机构表列表
	 */
	@PostMapping("/getOrgList")
	public JSONObject getOrgList(HttpServletRequest request) {
		return service.getOrgList(CommonUtil.request2Json(request));
	}

	/**
	 * 获取论文表列表
	 */
	@PostMapping("/getPaperList")
	public JSONObject getPaperList(HttpServletRequest request) {
		return service.getPaperList(CommonUtil.request2Json(request));
	}

	/**
	 * 获取专利表列表
	 */
	@PostMapping("/getPatentList")
	public JSONObject getPatentList(HttpServletRequest request) {
		return service.getPatentList(CommonUtil.request2Json(request));
	}

	/**
	 * 获取技术网络动态信息表列表
	 */
	@PostMapping("/getTechniqueArticleList")
	public JSONObject getTechniqueArticleList(HttpServletRequest request) {
		return service.getTechniqueArticleList(CommonUtil.request2Json(request));
	}

	/**
	 * 获取热词表列表
	 */
	@PostMapping("/getHotWordsList")
	public JSONObject getHotWordsList(HttpServletRequest request) {
		return service.getHotWordsList(CommonUtil.request2Json(request));
	}

	@PostMapping("/imgData")
	public JSONObject imgData(HttpServletRequest request) throws URISyntaxException {
		JSONObject resultJson = new JSONObject();
		resultJson.put("code", Constants.SUCCESS_RET);
		resultJson.put("msg", Constants.SUCCESS_MSG);
		Map<String,Object> map = new HashMap<>();
//		JSONObject data = new JSONObject();
//		data.put("data", resultJson);

//		//imgData = imgData.replaceAll(" ", "+");
//		 在"base64,"之后的才是图片信息


		String wordFilePath = this.getClass().getResource("/").toURI().getPath();
		String wordFileName ="导出.doc";
		String templatePath=wordFilePath;
		String templateFileName="autoReport.ftl";
		Map<String, Object> beanParams = new HashMap<>();

		String technologyCount_img = "imgData1";
		if(StringUtils.isNotEmpty(request.getParameter(technologyCount_img))){
			beanParams.put("technologyCount_img",String.valueOf(request.getParameter(technologyCount_img)).split("base64,")[1]);

		}

		String technologySourceDistribution_img = "imgData2";
		if(StringUtils.isNotEmpty(request.getParameter(technologySourceDistribution_img))){
			beanParams.put("technologySourceDistribution_img",String.valueOf(request.getParameter(technologySourceDistribution_img)).split("base64,")[1]);
		}
		WordUtil.writeWordReport(wordFilePath,wordFileName,templatePath,templateFileName,beanParams);
		return resultJson;
	}
}
